package cn.tedu.egweibo5.controller;

import cn.tedu.egweibo5.common.response.JsonResult;
import cn.tedu.egweibo5.common.response.StatusCode;
import cn.tedu.egweibo5.mapper.CommentMapper;
import cn.tedu.egweibo5.pojo.dto.CommentDTO;
import cn.tedu.egweibo5.pojo.entity.Comment;
import cn.tedu.egweibo5.pojo.entity.User;
import cn.tedu.egweibo5.pojo.vo.CommentVO;
import cn.tedu.egweibo5.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "评论模块")
@Slf4j
@RestController
@RequestMapping("/v1/comment/")
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;

    @ApiOperation("发布评论")
    @PostMapping("insert")
    public JsonResult insertComment(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
        /**
         * 1.判断登录
         * 2.插入insert
         */

        log.debug("commentDTO = " + commentDTO);

        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }

        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setUserId(userVO.getId());
        comment.setCreated(new Date());

        log.debug("comment: "+comment);

        commentMapper.insertComment(comment);

        return JsonResult.ok();

    }

    @ApiOperation("获取评论列表")
    @GetMapping("selectByweiboId")
    @ApiImplicitParam(name = "id", value = "微博id", dataType = "int", required = true)
    public JsonResult selectByweiboId(Integer id){

        log.debug("id = "+id);
        List<CommentVO> commentVOS = commentMapper.selectByweiboId(id);

        return JsonResult.ok(commentVOS);
    }
}
